<?php
/*Excel文件读取类*/
class CsvReader{
	
	/*读取文件相关设置*/
	public $parameter = array();
	
	
	public function get_lines(){

		/*引入文件读取类*/
		set_include_path(get_include_path() . PATH_SEPARATOR . './Classes/');

		/** PHPExcel */
		include 'PHPExcel.php';

		/** PHPExcel_IOFactory */
		include 'PHPExcel/IOFactory.php';
		
		//检测是否为excel文件
		if (!file_exists($this->parameter['path'])) {
			$txt = "excel 文件 no file"."\r\n";
			fwrite(myfile, $txt);
			return false;
		}
		
		$reader = PHPExcel_IOFactory::createReader($this->parameter['format']);

		$PHPExcel = $reader->load($this->parameter['path']); 

		$sheet = $PHPExcel->getSheet(0); 											//读取第一個工作表
		
		//检测是否设置所取行，未设置则自动获取行数
		if(!isset($this->parameter['highestRowS'])){
			$highestRow = $sheet->getHighestRow(); 									//自动取得总行数
		}else{
			$highestRow = $sheet->getHighestRow();
			$highestRow = $highestRow+$this->parameter['highestRowS'];
		}
		//检测是否设置所取行数，未设置则自动获取行数最大不能超过Y(25行)
		if(!isset($this->parameter['highestColumm'])){
			$this->parameter['highestColumm'] = $sheet->getHighestColumn(); 		//自动取得总列数
		}
		//检测是否设置初始读取开始列,未设置则从A列开始读取
		if(!isset($this->parameter['startColumn'])){
			$this->parameter['startColumn'] = 'A';
		}
		//检测是否设置初始读取开始行,未设置则从第一行开始读取
		if(!isset($this->parameter['KeyLine'])){
			$this->parameter['KeyLine'] = '1';
		}

		
		/** 循环读取每个单元格的数据 */
		$arr = array();

		for ($row = $this->parameter['startLine'];$row <= $highestRow;$row++){
			$dataset = array();
			for ($column = $this->parameter['startColumn']; $column <= $this->parameter['highestColumm']; $column++) {			

				$dataset[$sheet->getCell($column.$this->parameter['KeyLine'])->getValue()] = $sheet->getCell($column.$row)->getValue();
				//判断版本号是否存在
				if(isset($dataset[datver])){
					//将版本号对应时间与合规性字段合并到CSV读取的字段中
					$dataset = array_merge($dataset,$this->version_time($dataset[datver]));
				}else{
					$dataset = array_merge($dataset,$data = array('compliance'=>'未检查到版本号','date'=>'未检查到版本号'));
				}
			}
			$arr[] = $dataset;
		}
		return $arr;
	}
	
	//版本转换为时间
	public function version_time($versions){
		if(isset($versions)){
			
			$version = version;				//默认版本
			
			$time = time." 00:00:00";		//默认时间
			
			$oneDay = 86400;				//一天零点至24点时间戳

			$stamp = strtotime($time);
			
			$times = ($versions - $version)*$oneDay + $stamp;	//获取版本对应时间戳

			$data = array();
			
			$data['date'] = date('Y-m-d', $times);
			//检测版本时间的时间范围			a<=7		7<a<=14		14<a
			if($times >= strtotime(date("Y-m-d",strtotime("-7 day")))){
				$data['compliance'] ="7天以内";
				return $data;
			}else{
				if($times < strtotime(date("Y-m-d",strtotime("-14 day")))){
					$data['compliance'] ="14天以外";
					return $data;
				}else{
					$data['compliance'] ="7~14天以内";
					return $data;
				}
			}
		}else{
			return;
		}
	}
}
?>